/*
Copyright (c) [2019锟斤拷5锟斤拷1锟斤拷] [锟解超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#pragma once

#include "基础量.h"
#include "基本类型.h"
#include "几何数据.h"
#include "basic_vec.h"



#define MAT下标(m) ((float32*)(m))



inline float32 f_MAT下标(Mat44f* m, const uint8 id) { return ((float32*)m)[id]; }

Inline Mat44f f_mat44_identity() {
	static Mat44f mat = {
		1.0f, 0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		0.0f, 0.0f, 1.0f, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f,
	};

	return mat;
}
Inline mat3X4 f_mat34_identity() {
	static mat3X4 mat = {
		{1.0f, 0.0f, 0.0f, 0.0f},
		{0.0f, 1.0f, 0.0f, 0.0f},
		{0.0f, 0.0f, 1.0f, 0.0f},
	};
	return mat;
}
Inline mat4X3 f_mat43_identity() {
	static mat4X3 mat = {
		{1.0f, 0.0f, 0.0f},
		{0.0f, 1.0f, 0.0f},
		{0.0f, 0.0f, 1.0f},
		{0.0f, 0.0f, 0.0f},
	};
	return mat;
}


Inline Mat44f f_mat4_构建移动矩阵(const vec3& loc) {
	Mat44f mat = {
		1.0f, 0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		0.0f, 0.0f, 1.0f, 0.0f,
		loc.x, loc.y, loc.z, 1.0f,
	};
	return mat;
}

Inline Mat44f f_mat4_构建缩放矩阵(const vec3& s) {
	Mat44f mat = {
		s.x, 0.0f, 0.0f, 0.0f,
		0.0f, s.y, 0.0f, 0.0f,
		0.0f, 0.0f, s.z, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f,
	};
	return mat;
}


//矩阵拷贝
inline void Matrix4X4f_Copy_old_n1(const float mat[16], float toMat[16]){ for(uint32 i=0; i<16; ++i) toMat[i] = mat[i]; }
inline void Matrix4X4f_Copy(float toMat[16], const float mat[16]){ for(uint32 i=0; i<16; ++i) toMat[i] = mat[i]; }
inline void Matrix4X4f_Copy_3X3(const float mat[16], float toMat[16]){ for(uint32 i=0; i<12; ++i) toMat[i] = mat[i]; }
inline void Matrix4X4d_Copy(const double mat[16], double toMat[16]){ for(uint32 i=0; i<16; ++i) toMat[i] = mat[i]; }
inline void Matrix4X4d_Copy_3X3(const double mat[16], double toMat[16]){ for(uint32 i=0; i<12; ++i) toMat[i] = mat[i]; }

typedef struct _Mat3X3f {
	float32
		m00, m01, m02,
		m10, m11, m12,
		m20, m21, m22;
}Mat3X3f;


inline void f_构建单位矩阵(Mat44f* matA) {
	matA->m00 = 1.0; matA->m01 = 0.0; matA->m02 = 0.0; matA->m03 = 0.0;
	matA->m10 = 0.0; matA->m11 = 1.0; matA->m12 = 0.0; matA->m13 = 0.0;
	matA->m20 = 0.0; matA->m21 = 0.0; matA->m22 = 1.0; matA->m23 = 0.0;
	matA->m30 = 0.0; matA->m31 = 0.0; matA->m32 = 0.0; matA->m33 = 1.0;
}

inline void f_构建单位矩阵(Mat3X3f* matA) {
	matA->m00 = 1.0; matA->m01 = 0.0; matA->m02 = 0.0; 
	matA->m10 = 0.0; matA->m11 = 1.0; matA->m12 = 0.0; 
	matA->m20 = 0.0; matA->m21 = 0.0; matA->m22 = 1.0; 
}


inline void f_构建空白矩阵4X4(float32* matA) {
	for (uint8 i = 0; i < 16; ++i) matA[i] = 0.0;
}
inline void f_构建空白矩阵(Mat44f* matA) {
	matA->m00 = 0.0; matA->m01 = 0.0; matA->m02 = 0.0; matA->m03 = 0.0;
	matA->m10 = 0.0; matA->m11 = 0.0; matA->m12 = 0.0; matA->m13 = 0.0;
	matA->m20 = 0.0; matA->m21 = 0.0; matA->m22 = 0.0; matA->m23 = 0.0;
	matA->m30 = 0.0; matA->m31 = 0.0; matA->m32 = 0.0; matA->m33 = 0.0;
}
inline void f_构建空白矩阵(Mat3X3f* matA) {
	matA->m00 = 0.0; matA->m01 = 0.0; matA->m02 = 0.0; 
	matA->m10 = 0.0; matA->m11 = 0.0; matA->m12 = 0.0; 
	matA->m20 = 0.0; matA->m21 = 0.0; matA->m22 = 0.0; 
}

inline void f_矩阵拷贝f(Mat44f* mat, const Mat44f* matA) {
	for (uint8 i = 0; i < 16; ++i) MAT下标(mat)[i] = MAT下标(matA)[i];
}
inline void f_矩阵拷贝f(float32* mat, const float32* matA) {
	for (uint8 i = 0; i < 16; ++i) MAT下标(mat)[i] = MAT下标(matA)[i];
}
inline void f_矩阵转至f(float32* mat, const float32* matA) {
	for (uint8 ia = 0; ia < 4; ++ia) {
		for (uint8 ib = 0; ib < 4; ++ib) {
			MAT下标(mat)[ib*4 + ia] = MAT下标(matA)[ia*4 + ib];
		}
	}
}

#define MAT_BLANK(name)  Mat44f name; f_构建空白矩阵(&(name)) 
#define 矩阵_创建单位矩阵4X4(name)  Mat44f name; f_构建单位矩阵(&name) 
#define 矩阵_创建拷贝矩阵(name, mat)  Mat44f name; f_矩阵拷贝f((&name), (mat)) 

//填充单位矩阵
inline void Matrix4X4f_Identity(float matA[16]){
	matA[0] = 1.0; matA[1] = 0.0; matA[2] = 0.0; matA[3] = 0.0;
	matA[4] = 0.0; matA[5] = 1.0; matA[6] = 0.0; matA[7] = 0.0;
	matA[8] = 0.0; matA[9] = 0.0; matA[10] = 1.0; matA[11] = 0.0;
	matA[12] = 0.0; matA[13] = 0.0; matA[14] = 0.0; matA[15] = 1.0;

}
//填充单位矩阵
inline void Matrix4X4d_Identity(double matA[16]){
	matA[0] = 1.0; matA[1] = 0.0; matA[2] = 0.0; matA[3] = 0.0;
	matA[4] = 0.0; matA[5] = 1.0; matA[6] = 0.0; matA[7] = 0.0;
	matA[8] = 0.0; matA[9] = 0.0; matA[10] = 1.0; matA[11] = 0.0;
	matA[12] = 0.0; matA[13] = 0.0; matA[14] = 0.0; matA[15] = 1.0;
}

//This code comes directly from GLU except that it is for float
int f_Matrix4X4_InvertMatrixf2(const float *m, float *out);



//4*4矩阵相乘
inline void Matrix4X4_Mult(const float matA[16], const float matB[16], float matResult[16]){
	float tempData[4] = {0.0,0.0,0.0,0.0};

	tempData[0] = matA[0]*matB[0] + matA[4]*matB[1] + matA[8]*matB[2] + matA[12] * matB[3];
	tempData[1] = matA[0]*matB[4] + matA[4]*matB[5] + matA[8]*matB[6] + matA[12] * matB[7];
	tempData[2] = matA[0]*matB[8] + matA[4]*matB[9] + matA[8]*matB[10] + matA[12] * matB[11];
	tempData[3] = matA[0]*matB[12] + matA[4]*matB[13] + matA[8]*matB[14] + matA[12] * matB[15];
	matResult[0] = tempData[0]; matResult[4] = tempData[1]; matResult[8] = tempData[2]; matResult[12] = tempData[3];

	tempData[0] = matA[1]*matB[0] + matA[5]*matB[1] + matA[9]*matB[2] + matA[13] * matB[3];
	tempData[1] = matA[1]*matB[4] + matA[5]*matB[5] + matA[9]*matB[6] + matA[13] * matB[7];
	tempData[2] = matA[1]*matB[8] + matA[5]*matB[9] + matA[9]*matB[10] + matA[13] * matB[11];
	tempData[3] = matA[1]*matB[12] + matA[5]*matB[13] + matA[9]*matB[14] + matA[13] * matB[15];
	matResult[1] = tempData[0]; matResult[5] = tempData[1]; matResult[9] = tempData[2]; matResult[13] = tempData[3];

	tempData[0] = matA[2]*matB[0] + matA[6]*matB[1] + matA[10]*matB[2] + matA[14] * matB[3];
	tempData[1] = matA[2]*matB[4] + matA[6]*matB[5] + matA[10]*matB[6] + matA[14] * matB[7];
	tempData[2] = matA[2]*matB[8] + matA[6]*matB[9] + matA[10]*matB[10] + matA[14] * matB[11];
	tempData[3] = matA[2]*matB[12] + matA[6]*matB[13] + matA[10]*matB[14] + matA[14] * matB[15];
	matResult[2] = tempData[0]; matResult[6] = tempData[1]; matResult[10] = tempData[2]; matResult[14] = tempData[3];

	tempData[0] = matA[3]*matB[0] + matA[7]*matB[1] + matA[11]*matB[2] + matA[15] * matB[3];
	tempData[1] = matA[3]*matB[4] + matA[7]*matB[5] + matA[11]*matB[6] + matA[15] * matB[7];
	tempData[2] = matA[3]*matB[8] + matA[7]*matB[9] + matA[11]*matB[10] + matA[15] * matB[11];
	tempData[3] = matA[3]*matB[12] + matA[7]*matB[13] + matA[11]*matB[14] + matA[15] * matB[15];
	matResult[3] = tempData[1]; matResult[7] = tempData[1]; matResult[11] = tempData[2]; matResult[15] = tempData[3];

}
inline void Mat4X4_相乘(float 结果矩阵[16], const float matA[16], const float matB[16]) {
	Matrix4X4f_Identity(结果矩阵);
	float tempData[4] = { 0.0,0.0,0.0,0.0 };

	tempData[0] = matA[0] * matB[0] + matA[4] * matB[1] + matA[8] * matB[2] + matA[12] * matB[3];
	tempData[1] = matA[0] * matB[4] + matA[4] * matB[5] + matA[8] * matB[6] + matA[12] * matB[7];
	tempData[2] = matA[0] * matB[8] + matA[4] * matB[9] + matA[8] * matB[10] + matA[12] * matB[11];
	tempData[3] = matA[0] * matB[12] + matA[4] * matB[13] + matA[8] * matB[14] + matA[12] * matB[15];
	结果矩阵[0] = tempData[0]; 结果矩阵[4] = tempData[1]; 结果矩阵[8] = tempData[2]; 结果矩阵[12] = tempData[3];

	tempData[0] = matA[1] * matB[0] + matA[5] * matB[1] + matA[9] * matB[2] + matA[13] * matB[3];
	tempData[1] = matA[1] * matB[4] + matA[5] * matB[5] + matA[9] * matB[6] + matA[13] * matB[7];
	tempData[2] = matA[1] * matB[8] + matA[5] * matB[9] + matA[9] * matB[10] + matA[13] * matB[11];
	tempData[3] = matA[1] * matB[12] + matA[5] * matB[13] + matA[9] * matB[14] + matA[13] * matB[15];
	结果矩阵[1] = tempData[0]; 结果矩阵[5] = tempData[1]; 结果矩阵[9] = tempData[2]; 结果矩阵[13] = tempData[3];

	tempData[0] = matA[2] * matB[0] + matA[6] * matB[1] + matA[10] * matB[2] + matA[14] * matB[3];
	tempData[1] = matA[2] * matB[4] + matA[6] * matB[5] + matA[10] * matB[6] + matA[14] * matB[7];
	tempData[2] = matA[2] * matB[8] + matA[6] * matB[9] + matA[10] * matB[10] + matA[14] * matB[11];
	tempData[3] = matA[2] * matB[12] + matA[6] * matB[13] + matA[10] * matB[14] + matA[14] * matB[15];
	结果矩阵[2] = tempData[0]; 结果矩阵[6] = tempData[1]; 结果矩阵[10] = tempData[2]; 结果矩阵[14] = tempData[3];

	tempData[0] = matA[3] * matB[0] + matA[7] * matB[1] + matA[11] * matB[2] + matA[15] * matB[3];
	tempData[1] = matA[3] * matB[4] + matA[7] * matB[5] + matA[11] * matB[6] + matA[15] * matB[7];
	tempData[2] = matA[3] * matB[8] + matA[7] * matB[9] + matA[11] * matB[10] + matA[15] * matB[11];
	tempData[3] = matA[3] * matB[12] + matA[7] * matB[13] + matA[11] * matB[14] + matA[15] * matB[15];
	结果矩阵[3] = tempData[1]; 结果矩阵[7] = tempData[1]; 结果矩阵[11] = tempData[2]; 结果矩阵[15] = tempData[3];

}
//4*4矩阵相乘
inline void Matrix4X4_Multf(float matResult[16], const float matA[16], const float matB[16]){
	matResult[0] = matA[0]*matB[0] +  matA[4]*matB[1] +  matA[8]*matB[2] + matA[12]*matB[3];
	matResult[4] = matA[0]*matB[4] +  matA[4]*matB[5] + matA[8]*matB[6] +  matA[12]*matB[7];
	matResult[8] = matA[0]*matB[8] +  matA[4]*matB[9] +  matA[8]*matB[10] + matA[12]*matB[11];
	matResult[12] = matA[0]*matB[12] + matA[4]*matB[13] + matA[8]*matB[14] + matA[12]*matB[15];
	matResult[1] = matA[1]*matB[0] +  matA[5]*matB[1] + matA[9]*matB[2] + matA[13]*matB[3];
	matResult[5] = matA[1]*matB[4] + matA[5]*matB[5]+ matA[9]*matB[6]+ matA[13]*matB[7];
	matResult[9] = matA[1]*matB[8] + matA[5]*matB[9] + matA[9]*matB[10] + matA[13]*matB[11];
	matResult[13] =matA[1]*matB[12] + matA[5]*matB[13] + matA[9]*matB[14] + matA[13]*matB[15];
	matResult[2] = matA[2]*matB[0] + matA[6]*matB[1] + matA[10]*matB[2] + matA[14]*matB[3];
	matResult[6]= matA[2]*matB[4] + matA[6]*matB[5] + matA[10]*matB[6] + matA[14]*matB[7];
	matResult[10] = matA[2]*matB[8] + matA[6]*matB[9] + matA[10]*matB[10] + matA[14]*matB[11];
	matResult[14] = matA[2]*matB[12] + matA[6]*matB[13] + matA[10]*matB[14] + matA[14]*matB[15];
	matResult[3] = matA[3]*matB[0] + matA[7]*matB[1] + matA[11]*matB[2] + matA[15]*matB[3];
	matResult[7] = matA[3]*matB[4] + matA[7]*matB[5] + matA[11]*matB[6] + matA[15]*matB[7];
	matResult[11] = matA[3]*matB[8] + matA[7]*matB[9] + matA[11]*matB[10] + matA[15]*matB[11];
	matResult[15] = matA[3]*matB[12] + matA[7]*matB[13] + matA[11]*matB[14] + matA[15]*matB[15];

}
/*
-------------00 01 02 03
|
|  ----------10 11 12 13
|  |
|  |  -------20 21 22 23
|  |  |
|  |  |  ----30 31 32 33
|  |  |  |	 |  |  |  |
00 10 20 30---  |  |  |
01 11 21 31------  |  |
02 12 22 32---------  |
03 12 23 33------------
*/
inline void Mat4X4_相乘(Mat44f* 结果矩阵, const Mat44f* matA, const Mat44f* matB) {
	for (uint8 i = 0; i < 4; ++i) {
		for (uint8 y = 0; y < 4; ++y) {
			for (uint8 x = 0; x < 4; ++x) {
				MAT下标(结果矩阵)[i * 4 + y] += MAT下标(matA)[(y+i)*4] + MAT下标(matB)[y*4 + x];
				//MAT下标(结果矩阵)[y * 4 + i] += MAT下标(matA)[y * 4 + x] * MAT下标(matB)[x * 4 + i];
			}
		}
	}
}
inline void Mat3X3_相乘(Mat3X3f* 结果矩阵, const Mat3X3f* matA, const Mat3X3f* matB) {
	for (uint8 i = 0; i < 3; ++i) {
		for (uint8 y = 0; y < 3; ++y) {
			for (uint8 x = 0; x < 3; ++x) {
				MAT下标(结果矩阵)[i * 3 + y] += MAT下标(matA)[x * 3 + i] + MAT下标(matB)[y * 3 + x];
			}
		}
	}
}
inline void Mat4X4_相乘(Mat44f* 结果矩阵, const Mat44f* mat) {
	//矩阵_创建拷贝矩阵(tmpMat, 结果矩阵);
	MAT_BLANK(tmpMat);
	for (uint8 i = 0; i < 4; ++i) {
		for (uint8 y = 0; y < 4; ++y) {
			for (uint8 x = 0; x < 4; ++x) {
				MAT下标(&tmpMat)[y * 4 + i] += MAT下标(结果矩阵)[x * 4 + i] * MAT下标(mat)[y * 4 + x];
				//MAT下标(&tmpMat)[y * 4 + i] += MAT下标(结果矩阵)[y * 4 + x] * MAT下标(mat)[x * 4 + i];
			}
		}
	}
	f_矩阵拷贝f(结果矩阵, &tmpMat);
}
inline void f_Mat4X4_相乘(float32* 结果矩阵, const float32* mat) {
	float32 tmpMat[16];  f_构建空白矩阵4X4(tmpMat);
	
	for (uint8 i = 0; i < 4; ++i) {
		for (uint8 y = 0; y < 4; ++y) {
			for (uint8 x = 0; x < 4; ++x) {
				//tmpMat[y * 4 + i] += 结果矩阵[y * 4 + i] * mat[i + 4 * x];
				tmpMat[y * 4 + i] += 结果矩阵[x * 4 + i] * mat[y * 4 + x];
			}
		}
	}
	memcpy(结果矩阵, tmpMat, sizeof(float32) * 16);
}
inline void f_Mat4X4_相乘(float32* 结果矩阵, const float32* matA, const float32* matB) {
	f_构建空白矩阵4X4(结果矩阵);

	for (uint8 i = 0; i < 4; ++i) {
		for (uint8 y = 0; y < 4; ++y) {
			for (uint8 x = 0; x < 4; ++x) {
				结果矩阵[y * 4 + i] += matA[x * 4 + i] * matB[y * 4 + x];
			}
		}
	}
}


#define DEF_矩阵相乘(mat, mat2) Mat4X4_相乘( ((Mat44f*)mat), (&mat2))




//矩阵转置
inline void Matrix4X4_Transpose(const float mat[16], float matResult[16]){
	matResult[0] = mat[0];
	matResult[4] = mat[1];
	matResult[8] = mat[2];
	matResult[12] = mat[3];

	matResult[1] = mat[4];
	matResult[5] = mat[5];
	matResult[9] = mat[6];
	matResult[13] = mat[7];

	matResult[2] = mat[8];
	matResult[6] = mat[19];
	matResult[10] = mat[10];
	matResult[14] = mat[11];

	matResult[3] = mat[12];
	matResult[7] = mat[13];
	matResult[11] = mat[14];
	matResult[15] = mat[15];

}

//构建旋转矩阵
inline void Matrix4X4_BuildTranformMat(float mat[16], const float tranform[3]){
	Matrix4X4f_Identity(mat);
	mat[12] = tranform[0];
	mat[13] = tranform[1];
	mat[14] = tranform[2];
}
inline void Matrix4X4_BuildXMat(float mat[16], const float loc) {
	Matrix4X4f_Identity(mat);
	mat[12] = loc;
}
inline void Matrix4X4_BuildYMat(float mat[16], const float loc) {
	Matrix4X4f_Identity(mat);
	mat[13] = loc;
}
inline void Matrix4X4_BuildZMat(float mat[16], const float loc) {
	Matrix4X4f_Identity(mat);
	mat[14] = loc;
}



inline void Mat4X4_构建移动矩阵(float32 mat[16], const float32 pos[3]) {
	Matrix4X4f_Identity(mat);
	mat[12] = pos[0];
	mat[13] = pos[1];
	mat[14] = pos[2];
}

inline void Mat4X4_构建移动矩阵(float32* mat, const vec3& pos) {
	Matrix4X4f_Identity(mat);
	mat[12] = pos.x;
	mat[13] = pos.y;
	mat[14] = pos.z;
}


//移动
inline void Matrix4X4_Location(float mat[16], const float pos[3]){
	mat[12] = mat[0] * pos[0] + mat[4] * pos[1] + mat[8]  * pos[2] + mat[12];
	mat[13] = mat[1] * pos[0] + mat[5] * pos[1] + mat[9]  * pos[2] + mat[13];
	mat[14] = mat[2] * pos[0] + mat[6] * pos[1] + mat[10] * pos[2] + mat[14];
	mat[15] = mat[3] * pos[0] + mat[7] * pos[1] + mat[11] * pos[2] + mat[15];
}
inline void Matrix4X4_Location4(float mat[16], const float pos[4]) {
	mat[12] = mat[0] * pos[0] + mat[4] * pos[1] + mat[8] * pos[2] + mat[12] * pos[3];
	mat[13] = mat[1] * pos[0] + mat[5] * pos[1] + mat[9] * pos[2] + mat[13] * pos[3];
	mat[14] = mat[2] * pos[0] + mat[6] * pos[1] + mat[10] * pos[2] + mat[14] * pos[3];
	mat[15] = mat[3] * pos[0] + mat[7] * pos[1] + mat[11] * pos[2] + mat[15] * pos[3];
}

inline void Matrix4X4_Tranformv(float mat[16], const float pos[3]){
	float tempMatrix[16];
	Matrix4X4f_Identity(tempMatrix);

	Matrix4X4_Location(tempMatrix, pos);
	Matrix4X4_Mult(mat, mat, tempMatrix);

}


inline void Matrix4X4f_addMove(float mat[16], const float pos[3]){
	mat[12] += pos[0];
	mat[13] += pos[1];
	mat[14] += pos[2];
}


inline void Matrix4X4_Tranform(float mat[16], const float x, const float y, const float z){
	float tempMatrix[16];
	Matrix4X4f_Identity(tempMatrix);
	tempMatrix[12] = x;
	tempMatrix[13] = y;
	tempMatrix[14] = z;
	//把移动矩阵很当前矩阵相乘
	Matrix4X4_Mult(mat, mat, tempMatrix);
}
inline void f_构建移动矩阵(Mat44f* mat, const float32 x, const float32 y, const float32 z) {
	f_构建单位矩阵(mat);
	mat->m30 = x;
	mat->m31 = y;
	mat->m32 = z;
}
inline void f_构建移动矩阵v(Mat44f* mat, const float32* pos) {
	f_构建单位矩阵(mat);
	mat->m30 = pos[0];
	mat->m31 = pos[1];
	mat->m32 = pos[2];
}
#define 矩阵_创建移动矩阵(name, x, y, z) Mat44f name; f_构建移动矩阵(&name, x, y, z)
#define 矩阵_创建移动矩阵v(name,v)  Mat44f (name); f_构建移动矩阵v( (&name), (v) ) 



//构建旋转矩阵
inline void Matrix4X4_BuildRotationMat(float mat[16], const float 旋转角度[3]){
	Matrix4X4f_Identity(mat);
	if(旋转角度[0]){
		mat[5] = (float32)cos(M_角度转弧度(旋转角度[0])); mat[9] = (float32)-sin(M_角度转弧度(旋转角度[0]));
		mat[6] = (float32)sin(M_角度转弧度(旋转角度[0])); mat[10] = (float32)cos(M_角度转弧度(旋转角度[0]));
	}
	if(旋转角度[1]){
		mat[0] = (float32)cos(M_角度转弧度(旋转角度[1])); mat[2] = (float32)sin(M_角度转弧度(旋转角度[1]));
		mat[8] = (float32)-sin(M_角度转弧度(旋转角度[1])); mat[10] = (float32)cos(M_角度转弧度(旋转角度[1]));
	}
	if(旋转角度[2]){
		mat[0] = (float32)cos(M_角度转弧度(旋转角度[2])); mat[1] = (float32)-sin(M_角度转弧度(旋转角度[2]));
		mat[4] = (float32)sin(M_角度转弧度(旋转角度[2])); mat[5] = (float32)cos(M_角度转弧度(旋转角度[2]));
	}
}
inline void Matrix4X4_BuildRotationXMat(float mat[16], const float x){
	Matrix4X4f_Identity(mat);
	mat[5] = (float32)cos(M_角度转弧度(x)); mat[6] = (float32)-sin(M_角度转弧度(x));
	mat[9] = (float32)sin(M_角度转弧度(x)); mat[10] = (float32)cos(M_角度转弧度(x));
}
inline void Matrix4X4_BuildRotationYMat(float mat[16], const float y){
	Matrix4X4f_Identity(mat);
	mat[0] = (float32)cos(M_角度转弧度(y)); mat[2] = (float32)sin(M_角度转弧度(y));
	mat[8] = (float32)-sin(M_角度转弧度(y)); mat[10] = (float32)cos(M_角度转弧度(y));
}
inline void Matrix4X4_BuildRotationZMat(float mat[16], const float z){
	Matrix4X4f_Identity(mat);
	mat[0] = (float32)cos(M_角度转弧度(z)); mat[1] = (float32)-sin(M_角度转弧度(z));
	mat[4] = (float32)sin(M_角度转弧度(z)); mat[5] = (float32)cos(M_角度转弧度(z));
}


//旋转矩阵
inline void Matrix4X4_Rotation(float mat[16], const float rot[3]){
	float rotMatrix[16];
	//Matrix4X4_BuildRotationMat(rotMatrix, rot);
	//X
	Matrix4X4_BuildRotationXMat(rotMatrix, rot[0]);
	Matrix4X4_Mult(mat, rotMatrix, mat);
	//Y
	Matrix4X4_BuildRotationYMat(rotMatrix, rot[1]);
	Matrix4X4_Mult(mat, rotMatrix, mat);
	//Z
	Matrix4X4_BuildRotationZMat(rotMatrix, rot[2]);
	Matrix4X4_Mult(mat, rotMatrix, mat);
	//Matrix4X4_Mult(mat, rotMatrix, mat);
}
/*inline void Matrix4X4_Rotation3(float mat[16], const float rot[3], const float mov[3]){
	float rotMatrix[16];
	//Matrix4X4_BuildRotationMat(rotMatrix, rot);
	//X
	Matrix4X4_BuildRotationXMat(rotMatrix, rot[0]);
	rotMatrix[12] = mov[0];
	Matrix4X4_Mult(mat, rotMatrix, mat);
	//Y
	Matrix4X4_BuildRotationYMat(rotMatrix, rot[1]);
	rotMatrix[13] = mov[1];
	Matrix4X4_Mult(mat, rotMatrix, mat);
	//Z
	Matrix4X4_BuildRotationZMat(rotMatrix, rot[2]);
	rotMatrix[14] = mov[2];
	Matrix4X4_Mult(mat, rotMatrix, mat);
	//Matrix4X4_Mult(mat, rotMatrix, mat);
}*/
#define 矩阵_创建X旋转矩阵(name,v)  Mat44f name; f_构建单位矩阵(&name); Matrix4X4_BuildRotationXMat((float32*)(&name), (v) ) 
#define 矩阵_创建Y旋转矩阵(name,v)  Mat44f name; f_构建单位矩阵(&name); Matrix4X4_BuildRotationYMat((float32*)(&name), (v) ) 
#define 矩阵_创建Z旋转矩阵(name,v)  Mat44f name; f_构建单位矩阵(&name); Matrix4X4_BuildRotationZMat((float32*)(&name), (v) ) 

//#define 矩阵_创建XYZ旋转矩阵(name, x, y, z) Mat44f name; f_构建单位矩阵(&name); Matrix4X4_Rotation3((float32*)&(name), (x), (y), (z))
#define 矩阵_创建XYZ旋转矩阵v(name,v)  Mat44f name; f_构建单位矩阵(&name); Matrix4X4_Rotation((float32*)(&name), v ) 



//构建缩放矩阵
inline void f_构建缩放矩阵(Mat44f* mat, float32 x, float32 y, float32 z) {
	f_构建单位矩阵(mat);
	mat->m00 = x; mat->m11 = y; mat->m32 = z; 
}
inline void f_构建缩放矩阵v(Mat44f* mat, float32* v) {
	f_构建单位矩阵(mat);
	mat->m00 = v[0]; mat->m11 = v[1]; mat->m32 = v[2];
}
#define 矩阵_创建缩放矩阵(name, x, y, z) Mat44f name; f_构建缩放矩阵(&name, x, y, z)
#define 矩阵_创建缩放矩阵v(name,v)  Mat44f name; f_构建缩放矩阵v(&name, v ) 


inline void Matrix4X4_BuildScale(float mat[16], const float scale[3]){
	Matrix4X4f_Identity(mat);
	mat[0] = scale[0]; mat[5] = scale[1]; mat[10] = scale[2]; mat[15] = 1.0f;
}
inline void Matrix4X4_BuildScaleX(float mat[16], const float scale){
	Matrix4X4f_Identity(mat);
	mat[0] = scale;
}
inline void Matrix4X4_BuildScaleY(float mat[16], const float scale){
	Matrix4X4f_Identity(mat);
	mat[5] = scale;
}
inline void Matrix4X4_BuildScaleZ(float mat[16], const float scale){
	Matrix4X4f_Identity(mat);
	mat[10] = scale;
}

//缩放矩阵
inline void Matrix4X4_Scale(float mat[16], const float scale[3]){
	float rotMatrix[16];
	Matrix4X4_BuildScale(rotMatrix, scale);
	Matrix4X4_Mult(mat, rotMatrix, mat);

	//Matrix4X4_BuildScaleX(rotMatrix, scale[0]);
	//Matrix4X4_Mult(mat, rotMatrix, mat);
	//Matrix4X4_BuildScaleY(rotMatrix, scale[1]);
	//Matrix4X4_Mult(mat, rotMatrix, mat);
	//Matrix4X4_BuildScaleZ(rotMatrix, scale[2]);
	//Matrix4X4_Mult(mat, rotMatrix, mat);
}


//变换向量
inline void Matrix4X4_TranformVec(float toVec[3], const float vec[3], const float mat[16]){
	toVec[0] = vec[0]*mat[0] + vec[1]*mat[4] + vec[2]*mat[8] + mat[12];
	toVec[1] = vec[0]*mat[1] + vec[1]*mat[5] + vec[2]*mat[9] + mat[13];
	toVec[2] = vec[0]*mat[2] + vec[1]*mat[6] + vec[2]*mat[10] + mat[14];

}

inline void Matrix4X4_TranformVec4(float *resultVec, const float *matrix, const float *pvector)
{
	resultVec[0] = matrix[0]*pvector[0] + matrix[4]*pvector[1] + matrix[8]*pvector[2] + matrix[12]*pvector[3];
	resultVec[1] = matrix[1]*pvector[0] + matrix[5]*pvector[1] + matrix[9]*pvector[2] + matrix[13]*pvector[3];
	resultVec[2] = matrix[2]*pvector[0] + matrix[6]*pvector[1] + matrix[10]*pvector[2] + matrix[14]*pvector[3];
	resultVec[3] = matrix[3]*pvector[0] + matrix[7]*pvector[1] + matrix[11]*pvector[2] + matrix[15]*pvector[3];
}

/*inline vec4 f_Mat4X4_TranformVec4(const mat4& matrix, const vec4& pvector) {
	vec4 resultVec;

	resultVec.x = matrix.m0.x * pvector.x + matrix[4] * pvector[1] + matrix[8] * pvector[2] + matrix[12] * pvector[3];
	resultVec[1] = matrix[1] * pvector[0] + matrix[5] * pvector[1] + matrix[9] * pvector[2] + matrix[13] * pvector[3];
	resultVec[2] = matrix[2] * pvector[0] + matrix[6] * pvector[1] + matrix[10] * pvector[2] + matrix[14] * pvector[3];
	resultVec[3] = matrix[3] * pvector[0] + matrix[7] * pvector[1] + matrix[11] * pvector[2] + matrix[15] * pvector[3];
}*/


inline void Mat变换坐标3(const float32* mat, const float32* 点, float32* 结果) {
	for (uint8 横向 = 0; 横向 < 3; ++横向) {
		for (uint8 纵向 = 0; 纵向 < 4; ++纵向) {
			结果[横向] += mat[纵向 * 4 + 横向] * 点[横向];
		}
	}
}
inline void Mat变换坐标4(const float32* mat, const float32* 点, float32* 结果) {
	for (uint8 横向 = 0; 横向 < 4; ++横向) {
		for (uint8 纵向 = 0; 纵向 < 4; ++纵向) {
			结果[横向] += mat[纵向 * 4 + 横向] * 点[纵向];
		}
	}
}
inline void Mat变换坐标3X3(const float32* mat, const float32* 点, float32* 结果) {
	for (uint8 横向 = 0; 横向 < 3; ++横向) {
		for (uint8 纵向 = 0; 纵向 < 3; ++纵向) {
			结果[横向] += mat[纵向 * 3 + 横向] * 点[纵向];
		}
	}
}



#define MAT变换坐标3(m, v, r) Mat变换坐标3((float32*)(m), (float32*)(v), (float32*)(r))
#define MAT变换坐标4(m, v, r) Mat变换坐标4((float32*)(m), (float32*)(v), (float32*)(r))



